package framework.sepal4java.common.struct;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class ActorInfo {
    public String name;
    public ValueType type;
    public State state;
    public List<String> apps;
    private JSONObject jo;

    public ActorInfo(String name, ValueType type, State state, List<String> apps) {
        this.state = state;
        this.type = type;
        this.name = name;
        this.apps = apps;
    }

    public ActorInfo(JSONObject obj) {
        this.jo = obj;
        this.state = State.fromString(obj.getString("state"));
        this.type = ValueType.fromString(obj.getString("value_type"));
        this.name = obj.getString("actor_name");
        JSONArray ja = obj.getJSONArray("apps");
        apps = ja.toJavaList(String.class);
    }

    @Override
    public String toString() {
        if (jo != null) return jo.toJSONString();
        jo = new JSONObject();
        jo.put("actor_name", name);
        jo.put("value_type", type.toString());
        jo.put("state", state.toString());
        JSONArray ja = new JSONArray();
        ja.addAll(apps);
        jo.put("apps", ja);
        return jo.toJSONString();
    }
}
